package com.qing.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.eduservice.entity.dto.EduTeacherDto;
import com.qing.eduservice.entity.pojo.EduTeacher;
import com.qing.eduservice.entity.vo.EduTeacherVo;
import com.qing.eduservice.mapper.EduTeacherMapper;
import com.qing.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.servicebase.exception.JsonException;
import com.qing.servicebase.utils.ResponseResult;
import com.qing.servicebase.utils.SnowFlakeIdWorker;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-06
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    private final EduTeacherMapper eduTeacherMapper;
    private final SnowFlakeIdWorker snowFlakeIdWorker;

    public EduTeacherServiceImpl(EduTeacherMapper eduTeacherMapper, SnowFlakeIdWorker snowFlakeIdWorker) {
        this.eduTeacherMapper = eduTeacherMapper;
        this.snowFlakeIdWorker = snowFlakeIdWorker;
    }

    @Override
    public ResponseResult queryAllTeacher() {
        List<EduTeacher> eduTeachers = eduTeacherMapper.queryAllTeacher();
        return eduTeachers != null ? ResponseResult.ofSuccess(eduTeachers) : ResponseResult.ofException(new JsonException(201, "服务异常"));
    }

    @Override
    public ResponseResult deleteTeacherById(String id) {
        int result = eduTeacherMapper.deleteTeacherById(id);
        return result > 0 ? ResponseResult.ofSuccess("删除成功") : ResponseResult.ofException(new JsonException(201, "服务异常"));
    }

    @Override
    public ResponseResult createTeacher(EduTeacher eduTeacher) {
        /**
         * TODO
         * 添加前查询有无此老师  name做主键
         */
        eduTeacher.setId(Long.toString(snowFlakeIdWorker.nextId()));
        eduTeacher.setGmtCreate(LocalDateTime.now());
        eduTeacher.setGmtModified(LocalDateTime.now());
        int result = eduTeacherMapper.insert(eduTeacher);
        return result > 0 ? ResponseResult.ofSuccess("添加成功") : ResponseResult.ofException(new JsonException(201, "服务异常"));
    }

    @Override
    public ResponseResult updateTeacherById(EduTeacher eduTeacher) {
        int result = eduTeacherMapper.updateTeacherById(eduTeacher);
        return result > 0 ? ResponseResult.ofSuccess("修改成功") : ResponseResult.ofException(new JsonException(201, "服务异常"));
    }

    @Override
    public ResponseResult queryBookByPage(int page, int pageSize, EduTeacherVo eduTeacherVo) {
        if (page <= 0 || pageSize <= 0) {
            return ResponseResult.ofException(new JsonException(201, "服务异常"));
        }
        Page<EduTeacher> pages = new Page<>(page, pageSize);
        IPage<EduTeacher> eduTeacherIPage = eduTeacherMapper.queryBookByPage(pages,eduTeacherVo);
        return eduTeacherIPage != null ? ResponseResult.ofSuccess(eduTeacherIPage) : ResponseResult.ofException(new JsonException(201, "服务异常"));
    }

    @Override
    public ResponseResult searchTeacherById(String id) {
        EduTeacherDto eduTeacherDto = eduTeacherMapper.searchTeacherById(id);
        Map<String, EduTeacherDto> map = new HashMap<>();
        map.put("teacher",eduTeacherDto);
        return eduTeacherDto!=null?ResponseResult.ofSuccess(map):ResponseResult.ofException(new JsonException(201,"服务异常"));
    }

    @Override
    public ResponseResult pageListWeb(Page<EduTeacher> pageParam) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        baseMapper.selectPage(pageParam, queryWrapper);
        List<EduTeacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return ResponseResult.ofSuccess(map);
    }
}
